package cn.edu.just.firstjavaapp.l21;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Draw02Demo extends JPanel implements ActionListener {

    int[] sales = {45,166};
    final int  WIDTH = 50;
    final int BOTTOM = 500;
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(Color.lightGray);
        g.drawLine(10,BOTTOM,600,BOTTOM);
        g.setColor(Color.BLUE);
        g.fillRect(100,BOTTOM-sales[0],WIDTH,sales[0]);
        g.setColor(Color.orange);
        g.fillRect(100+100,BOTTOM-sales[1],WIDTH,sales[1]);

        System.out.println("paint is called");
    }



    public static void main(String[] args) {
        JFrame frame = new JFrame("我的画图");
        frame.setBounds(200,10,1000,600);
        Draw02Demo drawDemo = new Draw02Demo();
        drawDemo.setBackground(Color.white);
        frame.add(drawDemo,BorderLayout.CENTER);
        JButton change = new JButton("改变销量");
        change.addActionListener(drawDemo);
        frame.add(change,BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        sales[0] += Math.random()*10;
        sales[1] += Math.random()*10;
        repaint();
    }
}
